﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace SC.CodeBuilder
{
    partial class Builder
    {
        #region 生成DAL
        /// <summary>
        /// 生成DAL
        /// </summary>
        /// <returns></returns>
        public StringBuilder BuildeDALSB()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("using System;").AppendLine();
            sb.Append("using System.Data;").AppendLine();
            sb.Append("using System.Text;").AppendLine();
            sb.Append("using System.Collections.Generic;").AppendLine();
            sb.Append("using System.Data.SqlClient;").AppendLine();
            sb.Append("using System.Data.Common;").AppendLine();
            sb.Append("using System.Reflection;").AppendLine().AppendLine();
            sb.Append("using ").Append(unameSpace).Append("Model;").AppendLine();
            if (GetIDAL)
            {
                sb.Append("using ").Append(unameSpace).Append("IDAL;").AppendLine();
            }
            sb.Append("namespace ").Append(unameSpace).AppendLine("DAL");
            sb.Append("{").AppendLine();
            sb.Append("    /// <summary>").AppendLine();
            sb.Append("    /// 数据访问类").Append(tableName).AppendLine("。");
            sb.Append("    /// </summary>").AppendLine();
            if (GetIDAL)
            {
                sb.Append("    public partial class ").Append(tableName).Append(" : I").AppendLine(tableName);
            }
            else
            {
                sb.Append("    public partial class ").AppendLine(tableName);
            }
            sb.Append("    {").AppendLine();
            sb.Append("        public ").Append(tableName).AppendLine("() { }").AppendLine();
            //sb.AppendLine("        #region  成员方法");

            #region 基于Parameter方式
            if (true)// "Maticsoft BUilderDALParam"
            {
                if (Add) { BuilderDALAdd(sb); }
                if (Delete) { BuilderDALDelete(sb); }
                if (Update) { BuilderDALUpdate(sb); }
                if (GetModel) { BuilderDALGetModel(sb); }
                if (GetList) { BuilderDALGetModels(sb); }
                if (Exists) { BuilderDALExists(sb); }
                if (GetMaxID) { BuilderDALGetMaxID(sb); }
                if (GetRecordCount) { BuilderDALGetRecordCount(sb); }
                if (GetMaxID) { BuilderDALGetMaxID(sb); }
                if (Row2Model) { BuilderDALRow2Model(sb); }
                //else { BuilderDALRow2Model(sb); }
            }
            #endregion
            #region 基于企业库Param方式，暂时不使用

            //else //"BuilderDALELParam"
            //{
            //    #region Add
            //    if (Add)//Add
            //    {
            //        sb.AppendLine("        /// <summary>");
            //        sb.AppendLine("        /// 增加一条数据");
            //        sb.AppendLine("        /// </summary>");
            //        if (hasidentity)
            //        {
            //            sb.AppendLine("        public int Add(Model." + tableName + " model)");
            //        }
            //        else
            //        {
            //            sb.AppendLine("        public void Add(Model." + tableName + " model)");
            //        }

            //        sb.AppendLine("        {");
            //        sb.AppendLine("            StringBuilder strSql=new StringBuilder();");
            //        sb.AppendLine("            strSql.AppendLine(\"insert into " + tableName + "(\')' + ";");

            //        sb.AppendLine("            strSql.AppendLine(" + '"');
            //        List<string> list1 = new List<string>();
            //        foreach (DataRow dr in Columns.Tables[0].Rows)
            //        {
            //            if (dr[5].ToString() != "int identity")
            //            {
            //                list1.Add(dr[3].ToString());
            //            }
            //        }
            //        sb.AppendLine(string.Join(",", list1.ToArray()));
            //        sb.AppendLine(")\");");
            //        sb.AppendLine("            strSql.AppendLine(\" values (\");");
            //        sb.AppendLine("            strSql.AppendLine(" + '"');

            //        List<string> list2 = new List<string>();
            //        foreach (DataRow dr in Columns.Tables[0].Rows)
            //        {
            //            if (dr[5].ToString() != "int identity")
            //            {
            //                list2.Add("@" + dr[3].ToString());
            //            }
            //        }
            //        sb.AppendLine(string.Join(",", list2.ToArray()));
            //        sb.AppendLine(")\");");
            //        if (hasidentity)
            //        {
            //            sb.AppendLine("            strSql.AppendLine(\";select @@IDENTITY\");");
            //        }
            //        sb.AppendLine("            Database db = DatabaseFactory.CreateDatabase();");
            //        sb.AppendLine("            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());");
            //        //zhe  xia  mian ;
            //        foreach (DataRow dr in Columns.Tables[0].Rows)
            //        {
            //            if (dr[5].ToString() != "int identity")
            //            {

            //                sb.AppendLine("            db.AddInParameter(dbCommand, \dr[3].ToString() + '"' + ",DbType." + BuilderDALELParam.ConvertType(dr[5].ToString()) + ",model." + dr[3].ToString() + ");");

            //            }
            //        }

            //        if (hasidentity)
            //        {
            //            sb.AppendLine("            int result;");
            //            sb.AppendLine("            object obj = db.ExecuteScalar(dbCommand);");
            //            sb.AppendLine("            if(!int.TryParse(obj.ToString(),out result))");
            //            sb.AppendLine("            {");
            //            sb.AppendLine("                return 0 ;");
            //            sb.AppendLine("            }");
            //            sb.AppendLine("            return result;");

            //        }
            //        else
            //        {
            //            //sb.AppendLine("            db.ExecuteNonExecuteDataSet(dbCommand);");
            //            sb.AppendLine("            int result = db.ExecuteNonExecuteDataSet(dbCommand);");
            //            sb.AppendLine("            return result;");
            //        }
            //        sb.AppendLine("        }");
            //    }
            //    #endregion
            //    #region Delete
            //    if (Delete)//Delete
            //    {
            //        //完成
            //        sb.AppendLine("        /// <summary>");
            //        sb.AppendLine("        /// 删除一条数据");
            //        sb.AppendLine("         /// </summary>");


            //        //自增&主键
            //        if (!string.IsNullOrEmpt(identityName))
            //        {
            //            sb.AppendLine("        public void Delete(").Append(identityType).Append(" " + identityName.ToString().Trim() + ")");
            //            sb.AppendLine("        {");
            //            sb.AppendLine("            StringBuilder strSql=new StringBuilder();");
            //            sb.AppendLine("            strSql.AppendLine(\"delete " + tableName + " \");");
            //            sb.AppendLine("            strSql.AppendLine(\" where " + identityName + "=@" + identityName + '"' + ");");
            //            sb.AppendLine("            Database db = DatabaseFactory.CreateDatabase();");
            //            sb.AppendLine("            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());");
            //            sb.AppendLine("            db.AddInParameter(dbCommand, \identityName + '"' + ", DbType.Int32," + identityName + ");");
            //            sb.AppendLine("            int rcount = db.ExecuteNonExecuteDataSet(dbCommand);");
            //            sb.AppendLine("            return (db.ExecuteNonExecuteDataSet(dbCommand) > 0);");
            //            sb.AppendLine("        }");
            //        }
            //        //自增&主键
            //        else if (keys.Count != 0)
            //        {
            //            string DataType2 = "";
            //            string DataType3 = "";
            //            int DataLength = 0;
            //            foreach (DataRow dr in Columns.Tables[0].Rows)
            //            {
            //                if (dr[3].ToString() == keys[0])
            //                {
            //                    DataType2 = DataType.ConvertType(dr[5].ToString());
            //                    DataType3 = BuilderDALELParam.ConvertType(dr[5].ToString());
            //                    DataLength = Convert.ToInt32(dr[7].ToString());
            //                    break;
            //                }
            //            }
            //            sb.AppendLine("        public void Delete(" + DataType2 + " " + keys[0] + " )");
            //            sb.AppendLine("        {");
            //            sb.AppendLine("            StringBuilder strSql=new StringBuilder();");
            //            sb.AppendLine("            strSql.AppendLine(\"delete " + tableName + " \");");
            //            sb.AppendLine("            strSql.AppendLine(\" where " + keys[0] + "=@" + keys[0] + '"' + ");");
            //            sb.AppendLine("            Database db = DatabaseFactory.CreateDatabase();");
            //            sb.AppendLine("            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());");
            //            sb.AppendLine("            db.AddInParameter(dbCommand, \keys[0] + '"' + ", DbType." + DataType3 + "," + keys[0] + ")};");
            //            sb.AppendLine("            return (db.ExecuteNonExecuteDataSet(dbCommand) > 0);");
            //            sb.AppendLine("        }");

            //        }
            //        //自增主键 全无
            //        else
            //        {

            //            sb.AppendLine("        public void Delete(");
            //            List<string> list1 = new List<string>();
            //            foreach (DataRow dr in Columns.Tables[0].Rows)
            //            {
            //                list1.Add(DataType.ConvertType(dr[5].ToString() + " " + dr[3].ToString() + " "));

            //            }
            //            sb.AppendLine(string.Join(", ", list1.ToArray()));

            //            sb.AppendLine(")");
            //            sb.AppendLine("        {");
            //            sb.AppendLine("            StringBuilder strSql=new StringBuilder();");
            //            sb.AppendLine("            strSql.AppendLine(\"delete " + tableName + " \");");
            //            sb.AppendLine("            strSql.AppendLine(\"where ");
            //            if (identityName == null)
            //            {
            //                List<string> list2 = new List<string>();
            //                foreach (DataRow dr in Columns.Tables[0].Rows)
            //                {
            //                    list2.Add(dr[3].ToString() + "=@" + dr[3].ToString());

            //                }
            //                sb.AppendLine(string.Join(" and ", list2.ToArray()));

            //                sb.AppendLine(" \");");
            //            }
            //            sb.AppendLine("            Database db = DatabaseFactory.CreateDatabase();");
            //            sb.AppendLine("            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());");
            //            foreach (DataRow dr in Columns.Tables[0].Rows)
            //            {
            //                if (dr[5].ToString() != "int identity")
            //                {
            //                    sb.AppendLine("            db.AddInParameter(dbCommand, \dr[3].ToString() + '"' + ", DbType." + BuilderDALELParam.ConvertType(dr[5].ToString()) + "," + dr[3].ToString() + ");");
            //                }
            //            }
            //            sb.AppendLine("            return (db.ExecuteNonExecuteDataSet(dbCommand) > 0);");
            //            sb.AppendLine("         }");
            //        }


            //    }
            //    #endregion
            //    #region Update
            //    if (Update)//Update
            //    {
            //        sb.AppendLine("         /// <summary>");
            //        sb.AppendLine("         /// 更新一条数据");
            //        sb.AppendLine("         /// </summary>");
            //        sb.AppendLine("         public bool UpDate(Model." + tableName + " model)");
            //        sb.AppendLine("         {");
            //        sb.AppendLine("             StringBuilder strSql=new StringBuilder();");
            //        sb.AppendLine("             strSql.AppendLine(\"update " + tableName + " set\')' + ";");

            //        sb.AppendLine("             strSql.AppendLine(" + '"');
            //        List<string> list1 = new List<string>();
            //        foreach (DataRow dr in Columns.Tables[0].Rows)
            //        {
            //            if (dr[5].ToString() != "int identity")
            //            {
            //                list1.Add(dr[3].ToString() + "=@" + dr[3].ToString());
            //            }
            //        }
            //        sb.AppendLine(string.Join(",", list1.ToArray()));
            //        sb.AppendLine('"' + ");");
            //        sb.AppendLine("             strSql.AppendLine(\" where ");

            //        //自增&主键
            //        if (!string.isnullorem(identityName))
            //        {
            //            sb.AppendLine(identityName + "=@" + identityName + " \");");
            //        }
            //        else if (keys.Count != 0)//自增&主键
            //        {
            //            sb.AppendLine(keys[0] + "=@" + keys[0] + " \");");
            //        }
            //        else if (identityName == null && keys.Count == 0) //自增主键 全无
            //        {
            //            List<string> list2 = new List<string>();
            //            foreach (DataRow dr in Columns.Tables[0].Rows)
            //            {
            //                list2.Add(dr[3].ToString() + "=@" + dr[3].ToString());
            //            }

            //            sb.AppendLine(string.Join(" and ", list2.ToArray()));
            //            sb.AppendLine(" \");");
            //        }



            //        sb.AppendLine("             Database db = DatabaseFactory.CreateDatabase();");
            //        sb.AppendLine("             DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());");
            //        foreach (DataRow dr in Columns.Tables[0].Rows)
            //        {
            //            sb.AppendLine("             db.AddInParameter(dbCommand,\dr[3].ToString() + '"' + ",DbType." + BuilderDALELParam.ConvertType(dr[5].ToString()) + ", model." + dr[3].ToString() + ");");
            //        }
            //        sb.AppendLine("             return (db.ExecuteNonExecuteDataSet(dbCommand) > 0);");
            //        sb.AppendLine("         }");
            //    }
            //    #endregion
            //}
            #endregion

            //sb.AppendLine("        #endregion  成员方法");
            sb.AppendLine("    }");
            sb.AppendLine("}");
            return sb;
        }
        #endregion

        #region 增加一条数据 - void BuilderDALAdd(StringBuilder sb)
        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="sb"></param>
        void BuilderDALAdd(StringBuilder sb)
        {
            sb.AppendLine();
            sb.Append("        #region 增加一条数据").AppendLine();
            sb.Append("        /// <summary>").AppendLine();
            sb.Append("        /// 增加一条数据").AppendLine();
            sb.Append("        /// </summary>").AppendLine();
            if (hasidentity)
            {
                sb.Append("        public Model.").Append(tableName).Append(" Add(Model.").Append(tableName).AppendLine(" model)");
            }
            else
            {
                sb.Append("        public int Add(Model.").Append(tableName).AppendLine(" model)");
            }
            sb.Append("        {").AppendLine();
            sb.Append("            StringBuilder strSql = new StringBuilder();").AppendLine();
            sb.Append("            strSql.Append(\"insert into ").Append(tableName).Append("("); JoinDr(sb, false).Append(") values ("); JoinDrWithAt(sb, false).AppendLine(")\");");
            if (hasidentity)
            {
                sb.AppendLine("            strSql.Append(\";select @@IDENTITY\");");
            }
            if (hasidentity)
            {
                sb.Append("            object obj = ").Append(sqlName).AppendLine(".ExecuteScalar(strSql.ToString(),"); JoinSqlParameter(sb, false).AppendLine("                 );");
                sb.Append("            if (obj == null)").AppendLine();
                sb.Append("            {").AppendLine();
                sb.Append("                return model;").AppendLine();
                sb.Append("            }").AppendLine();
                sb.Append("            else").AppendLine();
                sb.Append("            {").AppendLine();
                if (identityType == "Guid")
                {
                    sb.Append("                model.").Append(UpperLetter(identityName)).Append(" = Guid.Parse(obj.ToString());").AppendLine();
                }
                else
                {
                    sb.Append("                model.").Append(UpperLetter(identityName)).Append(" = Convert.ToInt32(obj);").AppendLine();
                }
                sb.Append("                return model;").AppendLine();
                sb.Append("            }").AppendLine();
            }
            else
            {
                sb.Append("            return ").Append(sqlName).AppendLine(".ExecuteNonQuery(strSql.ToString(),"); JoinSqlParameter(sb, false).AppendLine("                 );");
            }
            sb.Append("        }").AppendLine();
            sb.Append("        #endregion").AppendLine();
        }
        #endregion

        #region 删除一条数据 - void BuilderDALDelete(StringBuilder sb)
        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="sb"></param>
        void BuilderDALDelete(StringBuilder sb)
        {
            sb.AppendLine();
            sb.Append("        #region 删除一条数据").AppendLine();
            sb.Append("        /// <summary>").AppendLine();
            sb.Append("        /// 删除一条数据").AppendLine();
            sb.Append("        /// </summary>").AppendLine();
            if (hasidentity)//自增&主键
            {
                sb.Append("        public bool Delete(").Append(identityType).Append(" ").Append(identityName).AppendLine(")//自增&主键");
                sb.Append("        {").AppendLine();
                sb.Append("            StringBuilder strSql = new StringBuilder();").AppendLine();
                sb.Append("            strSql.Append(\"delete ").Append(tableName).Append(" where ").Append(identityName).Append("=@").Append(identityName).AppendLine("\");");
                sb.Append("            SqlParameter[] parameters = { new SqlParameter(\"@").Append(identityName).AppendLine("\", SqlDbType.").Append(sqlTypeToSqlDbType(identityType)).Append(") };");
                sb.Append("            parameters[0].Value = ").Append(identityName).AppendLine(";");
                sb.Append("            return (").Append(sqlName).Append(".ExecuteNonQuery(strSql.ToString(), new SqlParameter(\"@").Append(identityName).Append("\", SqlDbType.").Append(sqlTypeToSqlDbType(identityType)).Append(") { Value = ").Append(identityName).AppendLine(" }) > 0);");
                sb.Append("        }").AppendLine();
            }
            else//无主键
            {
                sb.Append("        public bool Delete(Model.").Append(tableName).AppendLine(" model)//自增主键 全无");
                sb.Append("        {").AppendLine();
                sb.Append("            StringBuilder strSql = new StringBuilder();").AppendLine();
                sb.Append("            strSql.Append(\"delete ").Append(tableName).Append(" where "); JoinDrWithParameter(sb, false).AppendLine("\");");
                sb.Append("            return (").Append(sqlName).AppendLine(".ExecuteNonQuery(strSql.ToString(),"); JoinSqlParameter(sb, false).AppendLine("                 ) > 0);");
                sb.Append("        }").AppendLine();
            }
            sb.AppendLine("        #endregion");
        }
        #endregion

        #region 更新一条数据 - void BuilderDALUpdate(StringBuilder sb)
        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="sb"></param>
        void BuilderDALUpdate(StringBuilder sb)
        {
            sb.AppendLine();
            sb.Append("        #region 更新一条数据").AppendLine();
            sb.Append("        /// <summary>").AppendLine();
            sb.Append("        /// 更新一条数据").AppendLine();
            sb.Append("        /// </summary>").AppendLine();
            sb.Append("        public bool Update(Model.").Append(tableName).AppendLine(" model)");
            sb.Append("        {").AppendLine();
            sb.Append("            StringBuilder strSql = new StringBuilder();").AppendLine();
            sb.Append("            strSql.Append(\"update ").Append(tableName).Append(" set ");
            if (hasidentity)//自增&主键
            {
                JoinDrWithParameter(sb, false).Append(" where ").Append(identityName).Append("=@").Append(identityName).AppendLine("\");");
            }
            else //自增主键 全无
            {
                JoinDrWithParameter(sb, false).Append(" where "); JoinDrWithParameter(sb, false).AppendLine(" \");");
            }
            sb.Append("            return (").Append(sqlName).AppendLine(".ExecuteNonQuery(strSql.ToString(),"); JoinSqlParameter(sb, true).AppendLine("                 ) > 0);");
            sb.Append("        }").AppendLine();
            sb.Append("        #endregion").AppendLine();
        }
        #endregion

        #region 得到一个对象实体 - void BuilderDALGetModel(StringBuilder sb)
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="sb"></param>
        void BuilderDALGetModel(StringBuilder sb)
        {
            sb.AppendLine();
            sb.Append("        #region 得到一个对象实体").AppendLine();
            sb.Append("        /// <summary>").AppendLine();
            sb.Append("        /// 得到一个对象实体").AppendLine();
            sb.Append("        /// </summary>").AppendLine();
            if (hasidentity)//自增&主键
            {
                #region 自增
                sb.Append("        public ").Append(unameSpace).Append("Model.").Append(tableName).Append(" GetModel(").Append(identityType).Append(" ").Append(identityName).AppendLine(")//自增&主键");
                sb.Append("        {").AppendLine();
                sb.Append("            StringBuilder strSql = new StringBuilder();").AppendLine();
                sb.Append("            strSql.Append(\"select "); JoinDr(sb, true).Append(" from ").Append(tableName).Append(" where ").Append(identityName).Append("=@").Append(identityName).AppendLine("\");");
                sb.Append("            DataSet ds = ").Append(sqlName).Append(".ExecuteDataSet(strSql.ToString(), new SqlParameter(\"@").Append(identityName).Append("\", SqlDbType.").Append(sqlTypeToSqlDbType(identityType)).Append(") { Value = ").Append(identityName).AppendLine(" });");
                sb.Append("            if (ds.Tables[0].Rows.Count > 0)").AppendLine();
                sb.Append("            {").AppendLine();
                if (Row2Model)
                {
                    sb.AppendLine("                return Row2Model(ds.Tables[0].Rows[0]);");
                }
                else
                {
                    sb.Append("                return ").Append(sqlName).Append(".Row2Model<Model.").Append(tableName).AppendLine(">(ds.Tables[0].Rows[0]);");
                }
                sb.Append("            }").AppendLine();
                sb.Append("            else").AppendLine();
                sb.Append("            {").AppendLine();
                sb.Append("                return null;").AppendLine();
                sb.Append("            }").AppendLine();
                sb.Append("        }").AppendLine();
                #endregion
            }
            else
            {
                #region 自增 AND 主键 全无
                sb.Append("        public ").Append(unameSpace).Append("Model.").Append(tableName).Append(" GetModel("); JoinDrWithType(sb, false).AppendLine(")//自增主键 全无");
                sb.Append("        {").AppendLine();
                sb.Append("            StringBuilder strSql = new StringBuilder();").AppendLine();
                sb.Append("            strSql.Append(\"select "); JoinDr(sb, false).Append(" where "); JoinDrWithParameter(sb, false).AppendLine("\");");
                sb.Append("            DataSet ds = ").Append(sqlName).AppendLine(".ExecuteDataSet(strSql.ToString(),");
                JoinSqlParameterNoModel(sb, false);
                sb.Append("                );").AppendLine();
                sb.Append("            if (ds.Tables[0].Rows.Count > 0)").AppendLine();
                sb.Append("            {").AppendLine();
                if (Row2Model)
                {
                    sb.AppendLine("                return Row2Model(ds.Tables[0].Rows[0]);");
                }
                else
                {
                    sb.Append("                return ").Append(sqlName).Append(".Row2Model<Model.").Append(tableName).AppendLine(">(ds.Tables[0].Rows[0]);");
                }
                sb.Append("            }").AppendLine();
                sb.Append("            else").AppendLine();
                sb.Append("            {").AppendLine();
                sb.Append("                return null;").AppendLine();
                sb.Append("            }").AppendLine();
                sb.Append("        }").AppendLine();
                #endregion
            }
            sb.Append("        #endregion").AppendLine();
        }
        #endregion

        #region 获得数据列表 - void BuilderDALGetList(StringBuilder sb)
        /// <summary>
        /// 获得数据列表
        /// </summary>
        /// <param name="sb"></param>
        void BuilderDALGetModels(StringBuilder sb)
        {
            sb.AppendLine();
            sb.Append("        #region 获得数据列表").AppendLine();
            sb.AppendLine();

            sb.Append("        /// <summary>").AppendLine();
            sb.Append("        /// 获得数据列表").AppendLine();
            sb.Append("        /// </summary>").AppendLine();
            sb.Append("        /// <returns></returns>").AppendLine();
            sb.Append("        public List<Model.").Append(tableName).AppendLine("> GetModels()");
            sb.Append("        {").AppendLine();
            sb.Append("            return GetModels(null, 0, null);").AppendLine();
            sb.Append("        }").AppendLine();

            sb.Append("        /// <summary>").AppendLine();
            sb.Append("        /// 获得数据列表").AppendLine();
            sb.Append("        /// </summary>").AppendLine();
            sb.Append("        /// <param name=\"strWhere\">Where 条件</param>").AppendLine();
            sb.Append("        /// <param name=\"parameters\">参数</param>").AppendLine();
            sb.Append("        /// <returns></returns>").AppendLine();
            sb.Append("        public List<Model.").Append(tableName).AppendLine("> GetModels(string strWhere, params SqlParameter[] parameters)");
            sb.Append("        {").AppendLine();
            sb.Append("            return GetModels(strWhere, 0, null, parameters);").AppendLine();
            sb.Append("        }").AppendLine();

            sb.Append("        /// <summary>").AppendLine();
            sb.Append("        /// 获得数据列表").AppendLine();
            sb.Append("        /// </summary>").AppendLine();
            sb.Append("        /// <param name=\"top\">Top 条件</param>").AppendLine();
            sb.Append("        /// <param name=\"strWhere\">Where 条件</param>").AppendLine();
            sb.Append("        /// <param name=\"order\">Order 条件</param>").AppendLine();
            sb.Append("        /// <param name=\"parameters\">参数</param>").AppendLine();
            sb.Append("        /// <returns></returns>").AppendLine();
            sb.Append("        public List<Model.").Append(tableName).AppendLine("> GetModels(string strWhere, int top, string order, params SqlParameter[] parameters)");
            sb.Append("        {").AppendLine();
            sb.Append("            StringBuilder strSql = new StringBuilder();").AppendLine();
            sb.Append("            strSql.Append(\"select\");").AppendLine();
            sb.Append("            if (top > 0").AppendLine(") { strSql.Append(\" top \" + top); }");
            sb.Append("            strSql.Append(\" "); JoinDr(sb, true).Append(" FROM ").Append(tableName).AppendLine("\");");
            sb.Append("            if (!string.IsNullOrWhiteSpace(strWhere)) { strSql.Append(\" where \" + strWhere); }").AppendLine();
            sb.Append("            if (!string.IsNullOrWhiteSpace(order)) { strSql.Append(\" order by \" + order); }").AppendLine();
            sb.Append("            DataSet ds = ").Append(sqlName).AppendLine(".ExecuteDataSet(strSql.ToString(), parameters);");
            sb.Append("            List<Model.").Append(tableName).Append("> list = new List<Model.").Append(tableName).AppendLine(">();");
            sb.Append("            foreach (DataRow dr in ds.Tables[0].Rows)").AppendLine();
            sb.Append("            {").AppendLine();
            if (Row2Model)
            {
                sb.AppendLine("                list.Add(Row2Model(dr));");
            }
            else
            {
                sb.Append("                list.Add(").Append(sqlName).Append(".Row2Model<Model.").Append(tableName).AppendLine(">(dr));");
            }
            sb.Append("            }").AppendLine();
            sb.Append("            return list;").AppendLine();
            sb.Append("        }").AppendLine();
            sb.Append("        #endregion").AppendLine();

            sb.AppendLine();
            sb.AppendLine("        #region 分页获取数据列表");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 分页获取数据列表");
            sb.AppendLine("        /// </summary>");
            sb.Append("        public List<Model.").Append(tableName).AppendLine("> GetModels(string strWhere, string orderby, int pageIndex, int pageSize)");
            sb.AppendLine("        {");
            sb.AppendLine("            StringBuilder strSql = new StringBuilder();");
            sb.Append("            strSql.Append(\"SELECT * FROM ( SELECT ROW_NUMBER() OVER (order by T.").Append(identityName).Append(") AS Row, T.*  from ").Append(tableName).AppendLine(" T \");");
            sb.AppendLine("            if (!string.IsNullOrWhiteSpace(strWhere))");
            sb.AppendLine("            {");
            sb.AppendLine("                strSql.Append(\" WHERE \" + strWhere);");
            sb.AppendLine("            }");
            sb.AppendLine("            strSql.Append(\" ) AS TT\");");
            sb.AppendLine("            strSql.Append(string.Format(\" WHERE TT.Row between {0} and {1}\", (pageIndex - 1) * pageSize + 1, pageIndex * pageSize));");
            sb.Append("            DataSet ds = ").Append(sqlName).AppendLine(".ExecuteDataSet(strSql.ToString());");
            sb.Append("            List<Model.").Append(tableName).Append("> list = new List<Model.").Append(tableName).AppendLine(">();");
            sb.AppendLine("            foreach (DataRow dr in ds.Tables[0].Rows)");
            sb.AppendLine("            {");
            if (Row2Model)
            {
                sb.AppendLine("                list.Add(Row2Model(dr));");
            }
            else
            {
                sb.Append("                list.Add(").Append(sqlName).Append(".Row2Model<Model.").Append(tableName).AppendLine(">(ds.Tables[0].Rows[0]));");
            }
            sb.AppendLine("            }");
            sb.AppendLine("            return list;");
            sb.AppendLine("        }");
            sb.AppendLine("        #endregion");
        }
        #endregion

        #region 是否存在该记录 - void BuilderDALExists(StringBuilder sb)
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="sb"></param>   
        void BuilderDALExists(StringBuilder sb)
        {
            sb.AppendLine();
            sb.AppendLine("        #region 是否存在该记录");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 是否存在该记录");
            sb.AppendLine("        /// </summary>");

            if (hasidentity)//自增&主键
            {
                sb.Append("        public bool Exists(").Append(identityType).Append(" ").Append(identityName).AppendLine(")//自增&主键");
                sb.AppendLine("        {");
                sb.AppendLine("            StringBuilder strSql = new StringBuilder();");
                sb.Append("            strSql.Append(\"select count(1) from ").Append(tableName).Append(" where ").Append(identityName).Append("=@").Append(identityName).AppendLine(" \");");
                sb.Append("            return (").Append(sqlName).Append(".ExecuteNonQuery(strSql.ToString(), new SqlParameter(\"@").Append(identityName).Append("\", SqlDbType.").Append(sqlTypeToSqlDbType(identityType)).Append(") { Value = ").Append(identityName).Append(" }) > 0);");
                sb.AppendLine("        }");
            }
            else//自增主键 全无
            {
                sb.Append("        public bool Exists(Model.").Append(tableName).AppendLine(" model)//自增主键 全无");
                sb.AppendLine("        {");
                sb.AppendLine("            StringBuilder strSql = new StringBuilder();");
                sb.Append("            strSql.Append(\"select count(1) from ").Append(tableName).Append(" where "); JoinDrWithParameter(sb, false).AppendLine("\");");
                sb.Append("            return (").Append(sqlName).AppendLine(".ExecuteNonQuery(strSql.ToString(),"); JoinSqlParameter(sb, false).AppendLine("                 ) > 0);");
                sb.AppendLine("        }");
            }
            sb.AppendLine("        #endregion");
        }
        #endregion

        #region 获得记录总数 -void BuilderDALGetRecordCount(StringBuilder sb)
        /// <summary>
        /// 获得记录总数
        /// </summary>
        /// <param name="sb"></param>
        void BuilderDALGetRecordCount(StringBuilder sb)
        {
            sb.AppendLine();
            sb.AppendLine("        #region 获得记录总数");
            sb.AppendLine("        public int GetRecordCount()");
            sb.AppendLine("        {");
            sb.AppendLine("            return GetRecordCount(null);");
            sb.AppendLine("        }");

            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 获取记录总数");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        public int GetRecordCount(string strWhere)");
            sb.AppendLine("        {");
            sb.AppendLine("            StringBuilder strSql = new StringBuilder();");
            sb.Append("            strSql.Append(\"select count(1) FROM ").Append(tableName).AppendLine("\");");
            sb.AppendLine("            if (!string.IsNullOrWhiteSpace(strWhere)) { strSql.AppendLine(\" where \" + strWhere); }");
            sb.Append("            object obj = ").Append(sqlName).AppendLine(".ExecuteScalar(strSql.ToString());");
            sb.AppendLine("            if (obj == null)");
            sb.AppendLine("            {");
            sb.AppendLine("                return 0;");
            sb.AppendLine("            }");
            sb.AppendLine("            else");
            sb.AppendLine("            {");
            sb.AppendLine("                return Convert.ToInt32(obj);");
            sb.AppendLine("            }");
            sb.AppendLine("        }");
            sb.AppendLine("        #endregion");
        }
        #endregion

        #region 得到最大的ID - static void BuilderDALGetMaxID(StringBuilder sb)
        /// <summary>
        /// 得到最大的ID
        /// </summary>
        /// <param name="sb"></param>
        void BuilderDALGetMaxID(StringBuilder sb)
        {
            //sb.AppendLine("        #region 得到最大的ID");
            //sb.AppendLine("        #endregion");
        }
        #endregion

        #region 转换DataRow数据得到一个对象实体 + BuilderDALRow2Model(StringBuilder sb)
        /// <summary>
        /// 转换DataRow数据得到一个对象实体
        /// </summary>
        /// <param name="sb"></param>
        void BuilderDALRow2Model(StringBuilder sb)
        {
            sb.AppendLine();
            sb.Append("        #region 把包含 ").Append(tableName).Append(" 数据的DataRow转换成 Model.").Append(tableName).AppendLine(" 实体对象");
            sb.AppendLine("        /// <summary>");
            sb.Append("        /// 把包含 ").Append(tableName).Append(" 数据的DataRow转换成 Model.").Append(tableName).AppendLine(" 实体对象");
            sb.AppendLine("        /// </summary>");
            sb.Append("        /// <param name=\"dr\">包含 ").Append(tableName).AppendLine(" 数据的DataRow</param>");
            sb.Append("        /// <returns>Model.").Append(tableName).AppendLine("</returns>");
            sb.Append("        public static Model.").Append(tableName).AppendLine(" Row2Model(DataRow dr)");
            sb.AppendLine("        {");
            sb.Append("            Model.").Append(tableName).Append(" model = new Model.").Append(tableName).AppendLine("();");

            foreach (DataRow dr in Columns.Tables[0].Rows)
            {
                string dbType = dr[5].ToString().ToLower();
                string dr3 = dr[3].ToString();
                string dr3u = UpperLetter(dr3);
                sb.Append("            if (!dr.IsNull(\"").Append(dr3).Append("\")) { ");
                if (dbType == "int identity" || dbType == "int" || dbType == "bigint" || dbType == "smallint" || dbType == "tinyint")
                {
                    sb.Append("model.").Append(dr3u).Append(" = int.Parse(dr[\"").Append(dr3).Append("\"].ToString()); } else { model.").Append(dr3u).AppendLine(" = 0; }");
                }
                else if (dbType == "money" || dbType == "float")
                {
                    sb.Append("model.").Append(dr3u).Append(" = float.Parse(dr[\"").Append(dr3).Append("\"].ToString()); } else { model.").Append(dr3u).AppendLine(" = 0; }");
                }
                else if (dbType == "decimal" || dbType == "smallmoney")
                {
                    sb.Append("model.").Append(dr3u).Append(" = decimal.Parse(dr[\"").Append(dr3).Append("\"].ToString()); } else { model.").Append(dr3u).AppendLine(" = 0; }");
                }
                else if (dbType == "datetime" || dbType == "smdlldatetime")
                {
                    sb.Append("model.").Append(dr3u).Append(" = DateTime.Parse(dr[\"").Append(dr3).AppendLine("\"].ToString());}");
                }
                else if (dbType == "bit")
                {
                    sb.Append("model.").Append(dr3u).Append(" = bool.Parse(dr[\"").Append(dr3u).Append("\"].ToString()); } else { model.").Append(dr3u).AppendLine(" = false; }");
                }
                else if (dbType == "numeric" || dbType == "real")
                {
                    sb.Append("model.").Append(dr3u).Append(" = double.Parse(dr[\"").Append(dr3u).Append("\"].ToString()); } else { model.").Append(dr3u).AppendLine(" = 0; }");
                }
                else if (dbType == "binary" || dbType == "image" || dbType == "varbinary")
                {
                    sb.Append("model.").Append(dr3u).Append(" = (byte[])dr[\"").Append(dr3u).AppendLine("\"];");
                }
                else if (dbType == "uniqueidentifier")
                {
                    sb.Append("model.").Append(dr3u).Append(" = Guid.Parse(dr[\"").Append(dr3u).Append("\"].ToString()); } else { model.").Append(dr3u).AppendLine(" = default(Guid); }");
                }
                else
                {
                    sb.Append("model.").Append(dr3u).Append(" = dr[\"").Append(dr3).Append("\"].ToString(); } else { model.").Append(dr3u).AppendLine(" = string.Empty; }");
                }
            }

            sb.AppendLine("            return model;");
            sb.AppendLine("        }");
            sb.AppendLine("        #endregion");
        }
        #endregion

        #region 转换DataRow数据得到一个对象实体(泛型反射方式) + BuilderDALRow2ModelT(StringBuilder sb)
        /// <summary>
        /// 转换DataRow数据得到一个对象实体(泛型反射方式)
        /// </summary>
        /// <param name="sb"></param>
        void BuilderDALRow2ModelT(StringBuilder sb)
        {
            sb.AppendLine();
            sb.AppendLine("        #region 把包含 对象 数据的DataRow转换成 实体对象(泛型反射方式)");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 把包含 对象 数据的DataRow转换成 实体对象(泛型反射方式)");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <typeparam name=\"T\">源对象类型</typeparam>");
            sb.AppendLine("        /// <param name=\"dr\">包含 对象 数据的DataRow</param>");
            sb.AppendLine("        /// <returns></returns>");
            sb.AppendLine("        public static T Row2Model<T>(DataRow dr) where T : new()");
            sb.AppendLine("        {");
            sb.AppendLine("            T model = new T();");
            sb.AppendLine("            foreach (PropertyInfo p in (typeof(T)).GetProperties())");
            sb.AppendLine("            {");
            sb.AppendLine("                if (dr[p.Name] is System.DBNull) { break; }");
            sb.AppendLine("                p.SetValue(model, dr[p.Name], null);");
            sb.AppendLine("            }");
            sb.AppendLine("            return model;");
            sb.AppendLine("        }");
            sb.AppendLine("        #endregion");
        }
        #endregion
    }
}
